package com.controller;

import com.util.file.FileUtil;
import com.util.gen.ImageGenUtil;
import com.util.pdf.PdfUtil;
import com.util.zip.MutithreadZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Created by liangjiaming on 2020/12/29
 * @title
 * @Desc
 */
@RestController
@RequestMapping("/tool/v1")
@Slf4j
@Deprecated
public class ToolControllerV1 {

    @GetMapping("/getImage")
    public String getImage(@RequestParam Integer width, @RequestParam Integer height,
                           HttpServletResponse response, HttpServletRequest request) throws Exception {
        return ImageGenUtil.getImage(width, height);
    }

    @PostMapping("/getZipFile")
    public void getZipFile(@RequestParam(required = false) String srcDirPath, @RequestParam(required = false) String dstFilePath,
                           HttpServletResponse response, HttpServletRequest request) throws IOException {
        if (StringUtils.isBlank(srcDirPath)) {srcDirPath = "D:\\cpz\\raw";}
        log.info("原文件夹路径：{}", srcDirPath);
        if (StringUtils.isBlank(dstFilePath)) {dstFilePath = "D:\\cpz\\target\\压缩包.zip";}
        log.info("生成压缩包路径：{}", dstFilePath);

        MutithreadZipUtil.createZip(srcDirPath, dstFilePath);

        FileUtil.fileDownload(request, response, dstFilePath, "压缩包.zip");
    }

    @PostMapping("/getImagePdf")
    public void getImagePdf(@RequestParam(required = false) String srcDirPath, @RequestParam(required = false) String dstFilePath,
                            HttpServletResponse response, HttpServletRequest request) throws IOException {
        if (StringUtils.isBlank(srcDirPath)) {srcDirPath = "D:\\cpz\\imgs";}
        log.info("原图片文件夹路径：{}", srcDirPath);
        if (StringUtils.isBlank(dstFilePath)) {dstFilePath = "D:\\cpz\\target\\目标文件.pdf";}
        log.info("生成PDF路径：{}", dstFilePath);

        PdfUtil.imagesToPdf(dstFilePath, srcDirPath);

        FileUtil.fileDownload(request, response, dstFilePath, "目标文件.pdf");
    }

}
